var Showcase = require('../models').Showcase

/*创建showcase*/
exports.createShowcase = function(req,res){
  var showcasename = req.body.showcasename
  var showcase = new Showcase({name:showcasename})
  showcase.save(function(err){
    if(!err){
      res.json({status: "ok"})
    }
  })
}

/*将某个cheatsheet加入showcase*/
exports.addCheat2Case = function(req,res){
  var showcasename = req.params.showcasename
  var promise = Showcase.findOne({"name":showcasename}).exec()
  promise.then(function(showcase){
    showcase.cheatsheets.push(req.body.csid)
    showcase.save(function(err){
      if(!err){
        res.json({status: "ok"})
      }
    })
  })
}

/*从showcase中移除cheatsheet*/
exports.removeCheatFromCase = function(req,res){
  var showcasename = req.params.showcasename
  var promise = Showcase.findOne({"name":showcasename}).exec()
  promise.then(function(showcase){
    showcase.cheatsheets = showcase.cheatsheets.filter(function(a){return a != req.body.csid})
    showcase.save(function(err){
      if(!err){
        res.json({status: "ok"})
      }
    })
  })
}
